package at.codejedi.sto.gui.detail.chart;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.GridLayout;
import java.util.Hashtable;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class BattleDetailChart extends JPanel {

	private static final long serialVersionUID = 8628147049369199353L;

	private static JFreeChart createChart(XYDataset dataset, String title) {

		// create the chart...
		JFreeChart chart = ChartFactory.createXYLineChart(title, "Time [sec]", // domain
																				// axis
																				// label
				"Damage", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips?
				false // URLs?
				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

		// set the background color for the chart...
		chart.setBackgroundPaint(Color.white);

		// get a reference to the plot for further customisation...
		XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.white);

		// set the range axis to display integers only...
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		// disable bar outlines...
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
				.getRenderer();
		// renderer.setDrawBarOutline(false);

		// set up gradient paints for series...
		GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f,
				0.0f, new Color(0, 0, 64));
		GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f,
				0.0f, new Color(0, 64, 0));
		GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f,
				0.0f, new Color(64, 0, 0));
		renderer.setSeriesPaint(0, gp0);
		renderer.setSeriesPaint(1, gp1);
		renderer.setSeriesPaint(2, gp2);

		return chart;

	}

	private static XYDataset createDataset(List<BattleDetailChartData> data) {

		// create the dataset...

		XYSeriesCollection dataset = new XYSeriesCollection();
		for (BattleDetailChartData battleData : data) {
			String name = battleData.name;
			XYSeries series = new XYSeries(name);
			Hashtable<Long, Double> damage = battleData.damage;
			for (java.util.Map.Entry<Long, Double> entry : damage.entrySet()) {

				XYDataItem xyDataItem = new XYDataItem(
						(double) entry.getKey() / 1000,
						(double) entry.getValue());
				series.add(xyDataItem);
			}
			dataset.addSeries(series);
		}

		return dataset;

	}

	private String title;

	public BattleDetailChart(String title) {
		this.title = title;

	}

	public void init(List<BattleDetailChartData> data) {
		XYDataset dataset = createDataset(data);
		JFreeChart chart = createChart(dataset, title);
		ChartPanel chartPanel = new ChartPanel(chart, false);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setLayout(new GridLayout(1, 1));
		add(chartPanel);
	}

}
